package Controller;

import Model.CustomerDAO;
import Model.Customer;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Uchiha
 */
@WebServlet(name = "FormularioServlet", urlPatterns = {"/FormularioServlet"})
public class FormularioServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {



        CustomerDAO customerDAO = new CustomerDAO();
        String operation = request.getParameter("todo");

        //REGISTRAR
        if (operation.equalsIgnoreCase("Registrar")) {
            String name = request.getParameter("name");
            String lastname = request.getParameter("lastname");
            String address = request.getParameter("address");
            String phone = request.getParameter("phone");
            String email = request.getParameter("email");
            String sex = request.getParameter("sex");
            String password = request.getParameter("password");

            Customer customer = new Customer(name, lastname, address, phone, email, sex, password);


            customerDAO.addCustomer(customer);
            request.setAttribute("Accion", "Registrado");
            RequestDispatcher rd = request.getRequestDispatcher("validar.jsp");
            rd.forward(request, response);

            //EDITAR
        } else if (operation.equalsIgnoreCase("Editar")) {

            request.setAttribute("Customer", customerDAO.getCustomer((String) request.getParameter("id")));
            request.setAttribute("Accion", "Modificado");
            RequestDispatcher rd = request.getRequestDispatcher("Editar.jsp");
            rd.forward(request, response);

            //REMOVER    
        } else if (operation.equalsIgnoreCase("Remover")) {
            String x = (String) request.getParameter("id");
            customerDAO.deleteCostumer(x);

            request.setAttribute("lista", customerDAO.getAllCustomer());
            RequestDispatcher rd = request.getRequestDispatcher("Tabla.jsp");
            rd.forward(request, response);

            //TABLA
        } else if (operation.equalsIgnoreCase("Ver Tabla")) {
            request.setAttribute("lista", customerDAO.getAllCustomer());
            RequestDispatcher rd = request.getRequestDispatcher("Tabla.jsp");
            rd.forward(request, response);


        } else if (operation.equalsIgnoreCase("get")) {
            customerDAO.getCustomer(request.getParameter("id"));

        } else if (operation.equalsIgnoreCase("Guardar")) {

            Customer cust = customerDAO.getCustomer((String) request.getParameter("id"));
            cust.setName(request.getParameter("name"));
            cust.setLastname(request.getParameter("lastname"));
            cust.setAddress(request.getParameter("address"));
            cust.setPhone(request.getParameter("phone"));
            cust.setEmail(request.getParameter("email"));
            cust.setPassword(request.getParameter("password"));
            cust.setSex(request.getParameter("sex"));


            customerDAO.editCostumer(cust);
            request.setAttribute("Accion", "Modificado");
            RequestDispatcher rd = request.getRequestDispatcher("validar.jsp");
            rd.forward(request, response);


        } else if (operation.equalsIgnoreCase("Go")) {
            String texto = request.getParameter("texto");
            int size = texto.length();
            String selection = request.getParameter("Busqueda");

            if (selection.equalsIgnoreCase("name")) {
                   
            }
           
        }
    }
}
